/*
 MainMenuViewController.m
 Date Created: February 16, 2010
 
 Author: Karol Krizka <kkrizka@gmail.com>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "MainMenuViewController.h"
#import "AboutViewController.h"

#import "help/HLPViewController.h"
#import "sortinggame/SGHighScoresViewController.h"
#import "profilemngt/PMProfileManagementViewController.h"

@implementation MainMenuViewController

@synthesize profileList;

- (void)setProfileList:(PMProfileList *)newProfileList {
  if(profileList!=nil) 
  {
    [profileList release]; //Remove the old profile list
  }

  // Initialize the new profile list
  profileList=newProfileList;
  
  [self updateButtons];
}

- (void)viewDidLoad {
  [super viewDidLoad];
  
  self.title=@"Main Menu";
  
  // Make sure the back button displayed will say "Back"
  self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                 style:UIBarButtonItemStylePlain
                                                                target:nil 
                                                                action:nil];
  [self.navigationItem.backBarButtonItem release];
  
  [self updateButtons];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (IBAction)showNewGame:(id)sender {
  // Remove a previous game, if one exists
  if(sortingGameViewController) {
    [sortingGameViewController release];
    sortingGameViewController=nil;
  }
  
  // We must determine if this is the first time launching the sorting game
  // for this profile before passing it to the sorting game, because the sorting
  // game automatically changes its status.
  BOOL showHelp=!lastActiveProfile.sortingGameLaunched;
  // Create a new game, and show it
  sortingGameViewController=[[SGSortingGameViewController alloc] init];
  [sortingGameViewController setPlayerProfile:lastActiveProfile];
  
  if(showHelp) {
    // This is the first time the game is launched, so display the help section
    HLPViewController *hlpViewController=[[HLPViewController alloc] initWithImages:[NSArray arrayWithObjects:[UIImage imageNamed:@"aSGTutorialPage1.png"], 
                                                                                    [UIImage imageNamed:@"aSGTutorialPage2.png"], nil] ];
    [hlpViewController setNextViewController:sortingGameViewController];
  
    [[self navigationController] pushViewController:hlpViewController animated:YES];
  } else {
    [[self navigationController] pushViewController:sortingGameViewController animated:YES];
  }
}

- (IBAction)showResumeGame:(id)sender {
  if(sortingGameViewController == nil) return;
  
  // Restore previous game
  [[self navigationController] pushViewController:sortingGameViewController animated:YES];
}

- (IBAction)showRecycling:(id)sender {
  if(recyclingViewController==nil) {
    recyclingViewController=[[RecyclingViewController alloc] initWithProfile:lastActiveProfile];
    [recyclingViewController retain];
  }
  
	[[self navigationController] pushViewController:recyclingViewController animated:YES];
}

- (IBAction)showProfileMngt:(id)sender {
	PMProfileManagementViewController *profileManagementViewController=(PMProfileManagementViewController *)[[PMProfileManagementViewController alloc] initWithProfileList:[self profileList]];
	[[self navigationController] pushViewController:profileManagementViewController animated:YES];
	[profileManagementViewController release];
}

- (IBAction)showHighScores:(id)sender {
  SGHighScoresViewController *highScoresViewController=[[SGHighScoresViewController alloc] init];
  
  [highScoresViewController setManagedObjectContext:[[self profileList] managedObjectContext]];

  [[self navigationController] pushViewController:highScoresViewController animated:YES];
  [highScoresViewController release];
}

- (IBAction)showAbout:(id)sender {
  AboutViewController *aboutViewController=[[AboutViewController alloc] init];
  [self setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
  [self presentModalViewController:aboutViewController animated:YES];
  
  [aboutViewController release];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
	
  // Hide navigation bar
  [[self navigationController] setNavigationBarHidden:YES animated:animated];
  
  // Check if the sorting game is finished (aka no lives left)
  if([sortingGameViewController livesLeft]==0) {
    [sortingGameViewController release];
    sortingGameViewController=nil;
  }

  [self updateButtons];
}

- (void)updateButtons {
  if(![self isViewLoaded]) return; // Nothing to update if the view is not loaded yet

  PMProfile *activeProfile=[profileList activeProfile];
  if(activeProfile != lastActiveProfile) {
    NSString *title=[[NSString alloc] initWithFormat:@"Profile: %@",[activeProfile profileName]];
    // We have a new active profile, switch up stuff!
    
    // 1 - Change the name in the profile button
    [profileMngtButton setTitle:title 
                       forState:(UIControlState)UIControlStateNormal];
    
    // 2 - Reset any currently running games
    if(sortingGameViewController) {
      [sortingGameViewController release];
      sortingGameViewController=nil;
    }
    
    // 3 - Reset recycling view
    if(recyclingViewController) {
      [recyclingViewController release];
      recyclingViewController=nil;
    }    

    [lastActiveProfile release];
    lastActiveProfile=[activeProfile retain];
  }
  
  [resumeGameButton setEnabled:(sortingGameViewController!=nil)];
}

- (void)dealloc {
    [super dealloc];
}


@end
